/*
 * Copyright 2020 by OLTPBenchmark Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.oltpbenchmark.benchmarks.auctionmark.util;

import com.oltpbenchmark.util.CompositeId;
import java.util.Comparator;
import java.util.Objects;
import java.util.stream.IntStream;

public final class UserId extends CompositeId implements Comparable<UserId> {

  private static final int[] COMPOSITE_BITS = {
    INT_MAX_DIGITS, // ITEM_COUNT
    INT_MAX_DIGITS // OFFSET
  };

  public static final int ID_LENGTH = IntStream.of(COMPOSITE_BITS).sum();

  /** The size index is the position in the histogram for the number of users per items size */
  private int itemCount;

  /** The offset is based on the number of users that exist at a given size index */
  private int offset;

  /**
   * Constructor
   *
   * @param itemCount
   * @param offset
   */
  public UserId(int itemCount, int offset) {
    this.itemCount = itemCount;
    this.offset = offset;
  }

  /**
   * Constructor Converts a composite id generated by encode() into the full object
   *
   * @param composite_id
   */
  public UserId(String composite_id) {
    this.decode(composite_id);
  }

  @Override
  public String encode() {
    return (this.encode(COMPOSITE_BITS));
  }

  @Override
  public void decode(String composite_id) {
    String[] values = super.decode(composite_id, COMPOSITE_BITS);
    this.itemCount = Integer.parseInt(values[0]);
    this.offset = Integer.parseInt(values[1]);
  }

  @Override
  public String[] toArray() {
    return (new String[] {Integer.toString(this.itemCount), Integer.toString(this.offset)});
  }

  public int getItemCount() {
    return this.itemCount;
  }

  public int getOffset() {
    return this.offset;
  }

  @Override
  public String toString() {
    return String.format(
        "UserId<itemCount=%d, offset=%d, encoded=%s>", this.itemCount, this.offset, this.encode());
  }

  public static String toString(String userId) {
    return new UserId(userId).toString();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    UserId userId = (UserId) o;
    return itemCount == userId.itemCount && offset == userId.offset;
  }

  @Override
  public int hashCode() {
    return Objects.hash(itemCount, offset);
  }

  @Override
  public int compareTo(UserId o) {
    return Comparator.comparingInt(UserId::getItemCount)
        .thenComparingInt(UserId::getOffset)
        .compare(this, o);
  }
}
